package com.example.mymenu;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;

import com.example.mymenu.manager.DataManager;

import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity{
/*
* 1.获取到recyclerview控件对象
* 2.设计recyclerview控件的适配器，包括item视图的布局和数据内容都要在适配器中定义出来
* 3.设置recyclerview的布局管理器
* 创建适配器对象，将此对象交付给recyclerview控件对象
*
* */
    private RecyclerView mylist;
    private DataManager dataManager;
    private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取数据
        dataManager = new DataManager();
        String[] title = dataManager.getTitleMessage();
        Map<String,List<String>> information = dataManager.getInformation();
        //布局
        mylist = findViewById(R.id.mylist);
        mylist.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL));
        HomeAdapter homeAdapter = new HomeAdapter(MainActivity.this,information,title);
        mylist.setAdapter(homeAdapter);
        //网络请求数据
        String findMessage = dataManager.internetFindMessage();
        //启动服务
        intent = new Intent(MainActivity.this, MyService.class);
        intent.putExtra("findMessage",findMessage);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            this.startForegroundService(intent);
        } else {
            this.startService(intent);
        }
        //接收广播的部分
        //创建intent过滤器
        MyReceiver myReceiver = new MyReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.my.app");
        //注册
        registerReceiver(myReceiver,intentFilter);



    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopService(intent);
    }

}